package com.woniuxy.intellihome.controller.front;


import com.woniuxy.intellihome.entity.ResponseEntity;
import com.woniuxy.intellihome.entity.User;
import com.woniuxy.intellihome.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-08-11
 */
@Controller("front/user")
@RequestMapping("front/user")
public class UserController {
    @Autowired(required = true)
    private UserService userService;
    //进入登录页面
    @GetMapping("login")
    public String login(){
        return "login";
    }
    //进行登录操作
    @ResponseBody
    @PostMapping("login")
    public ResponseEntity login(@RequestBody User user, Model model, HttpSession session){
        User user1 = userService.getByAccount(user.getAccount());
        if (user1!=null){
            //该用户存在
            System.out.println("该用户存在!!!");
            //判断密码
            if(user1.getPassword().equals(user.getPassword())){
                //登录成功，跳转首页
                System.out.println("登录成功");
                session.setAttribute("user1",user1);
                //model.addAttribute("user",user);
                return new ResponseEntity(200,"登录成功");
            }else {
                //密码错误，登录失败，返回登录页面
                //session.setAttribute("msg","密码不正确");
                model.addAttribute("msg","密码不正确");
                return new ResponseEntity(400,"密码不正确");
            }
        }else {
            //该用户不存在，登录失败，返回登录页面
            System.out.println("该用户不存在");
            //session.setAttribute("msg","该用户不存在");
            model.addAttribute("msg","该用户不存在");
            return new ResponseEntity(401,"密码不正确");
        }
    }
    //进入首页
    @GetMapping("index")
    public String index(){
        return "redirect:/";
    }
    //进入用户注册页面
    @GetMapping("registered")
    public String registered(){
        return "registered";
    }

    //进行用户注册操作，注册成功跳转到登录页面，失败返回注册页面
    @ResponseBody
    @PostMapping("registered")
    public ResponseEntity registered(@RequestBody User user, Model model, HttpSession session){
        System.out.println(user);
        User user1 = userService.getByAccount(user.getAccount());
        User user2 = new User();
        if (user1!=null){
            System.out.println("该用户存在!");
            //session.setAttribute("msg","该用户不存在");
            model.addAttribute("msg","该用户存在!");
            return new ResponseEntity(400,"该用户名已存在");
        }else {
            //不存在该用户，再判断邮箱是否已存在
            if(userService.getUserByEmail(user.getEmail())!=null){
                //该邮箱已存在
                model.addAttribute("msg","该邮箱已存在!");
                return new ResponseEntity(401,"该邮箱已存在");
            }else {
                //进行登录操作
                user2.setAccount(user.getAccount());
                user2.setPassword(user.getPassword());
                user2.setEmail(user.getEmail());
                user2.setPicture("/images/111222.jpg");
                //注册积分加100
                user2.setScore(100);
                //注册时间
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                user2.setRegTime(df.format(new Date()));
                //验证码
                user2.setCode(user.getCode());
                //状态
                user2.setStatus("y");
                userService.add(user2);
                return new ResponseEntity(200,"登录成功");
            }
        }
    }
    @GetMapping("logout")
    private String logout(HttpSession session){
        session.removeAttribute("user1");
       return "redirect:/front/user/login";
    }


}

